package com.lxf.password;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 加密
 * @ClassName Encryption
 * @author lxf
 * @Date 2017年5月6日 下午10:07:02
 * @version 1.0.0
 */
public class Encryption {
    
    /**
     * MD5加密字符串
     * @param str   需要加密的字符串
     * @return  String 加密后的结果
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static String encoderMD5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        //确定计算方法
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(("jy"+str).getBytes());
        // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
        // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
        String m = new BigInteger(1, md5.digest()).toString(16);
        //再加密一次
        md5.update(("jy"+m).getBytes());
        return new BigInteger(1, md5.digest()).toString(16);
    }
    
    /**
     * 比较两个加密后的字符串是否一致MD5
     * @param newpasswd
     * @param oldpasswd
     * @return  boolean
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static boolean checkPasswordMD5(String newpasswd,String oldpasswd) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        if(encoderMD5(newpasswd).equals(oldpasswd))
            return true;
        else
            return false;
    }
    
    /**
     * SHA加密
     * @param str   待加密的字符串
     * @return String
     */
    public static String getSha1(String str){
        if(str==null||str.length()==0){
            return null;
        }
        char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
            'a','b','c','d','e','f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));

            byte[] md = mdTemp.digest();
            int j = md.length;
            char buf[] = new char[j*2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];      
            }
            return new String(buf);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 比较两个加密后的字符串是否一致Sha1
     * @param newpasswd
     * @param oldpasswd
     * @return boolean
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static boolean checkPasswordSha1(String newpasswd,String oldpasswd) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        if(getSha1(newpasswd).equals(oldpasswd))
            return true;
        else
            return false;
    }

    public static void main(String[] args) {
        System.out.println(getSha1("123"));
    }
}
